﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;

namespace He.Common
{
    /// <summary>
    /// 邮件发送帮助类
    /// </summary>
    public class EmailHelper
    {
        #region 发送电子邮件
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="fromUser">发送者邮件</param>
        /// <param name="toUser">接收者邮件</param>
        /// <param name="emailUser">发送者邮件用户名</param>
        /// <param name="emailPwd">发送者邮件密码</param>
        /// <param name="title">邮件标题</param>
        /// <param name="memoText">邮件内容</param>
        /// <param name="smtpServer">发送使用的SMTP邮件服务器</param>
        /// <param name="attachmentPath">附件的路径如E:\\abcd\\abc.doc</param>
        /// <returns>true:发送成功  false:发送失败</returns>
        public static bool SendEmail(string fromUser, string toUser, string emailUser, string emailPwd, string title, string memoText, string smtpServer, string attachmentPath)
        {
            //发送代码
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromUser, "Send Name", Encoding.GetEncoding("UTF-8"));
            mail.To.Add(new MailAddress(toUser, "Received Name", Encoding.GetEncoding("UTF-8")));
            mail.Subject = title;
            mail.Body = memoText;
            mail.IsBodyHtml = false;
            mail.SubjectEncoding = Encoding.GetEncoding("UTF-8");
            mail.BodyEncoding = Encoding.GetEncoding("UTF-8");
            mail.Attachments.Add(new Attachment(attachmentPath));
            mail.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient(smtpServer);
            smtp.Credentials = new NetworkCredential(emailUser, emailPwd);
            try
            {
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromTitle">发件人名称</param>
        /// <param name="fromEmail">发件人邮箱</param>
        /// <param name="fromPwd">发件人邮箱密码</param>
        /// <param name="smtpServer">SMTP服务器，如smtp.163.com smtp.qq.com等</param>
        /// <param name="toTitle">收件人名称</param>
        /// <param name="toEmail">收件人邮箱</param>
        /// <param name="mailSubject">邮件标题</param>
        /// <param name="mailBody">邮件内容</param>
        /// <param name="isHtml">邮件内容是否html</param>
        /// <returns></returns>
        public static bool SendEmail(string fromTitle, string fromEmail, string fromPwd, string smtpServer, string toTitle, string toEmail, string mailSubject, string mailBody, bool isHtml)
        {
            //发送代码
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromEmail, fromTitle, Encoding.GetEncoding("UTF-8"));
            mail.To.Add(new MailAddress(toEmail, toTitle, Encoding.GetEncoding("UTF-8")));
            mail.Subject = mailSubject;
            mail.Body = mailBody;
            mail.IsBodyHtml = isHtml;
            mail.SubjectEncoding = Encoding.GetEncoding("UTF-8");
            mail.BodyEncoding = Encoding.GetEncoding("UTF-8");
            //mail.Attachments.Add(new Attachment(attachmentPath));
            mail.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient(smtpServer);
            smtp.Credentials = new NetworkCredential(fromEmail, fromPwd);
            try
            {
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion

        #region 验证是否是email邮箱地址

        private readonly static Regex RegexEmail = new Regex("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");

        /// <summary>
        /// 验证是否是email邮箱地址
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns></returns>
        public static bool ValidEmail(string email)
        {
            return RegexEmail.IsMatch(email);
        }
        #endregion
    }
}
